package com.my.aslife.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.my.aslife.aop.UpdateSensitive;
import com.my.aslife.mapper.SensitiveMapper;
import com.my.aslife.module.common.Constant;
import com.my.aslife.module.common.PageRequestVo;
import com.my.aslife.module.common.RedisConstant;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.entity.Sensitive;
import com.my.aslife.service.SensitiveService;
import com.my.aslife.utils.CheckSensitiveUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author mayuan
 * @description TODO
 */
@Service
@Slf4j
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, Sensitive> implements SensitiveService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SensitiveMapper sensitiveMapper;

    @PostConstruct
    public void initSensitive() {
        Set<String> set = new HashSet<>();
        List<Sensitive> list = sensitiveMapper.listALL();
        for (Sensitive sensitive : list) {
            set.add(sensitive.getSensitive());
        }

        try {
            stringRedisTemplate.opsForValue().set(RedisConstant.SENSITIVE_CONTEXT,
                    new ObjectMapper().writeValueAsString(set));
            CheckSensitiveUtil.initMap(set);
        } catch (JsonProcessingException e) {
            log.error("敏感词序列化异常");
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult getSensitiveListInfo(PageRequestVo pageRequestVo) {
        pageRequestVo.setCurrent((pageRequestVo.getCurrent() - 1) * 10);
        List<Sensitive> list = sensitiveMapper.list(pageRequestVo);
        return ResponseResult.ok(list, count());
    }

    @Override
    @UpdateSensitive
    public ResponseResult deleteSensitiveById(Integer id) {
        sensitiveMapper.deleteById(id);
        return ResponseResult.ok();
    }

    @Override
    @UpdateSensitive
    public ResponseResult insertSensitive(Sensitive sensitive) {
        save(sensitive);
        return ResponseResult.ok();
    }

    @Override
    @UpdateSensitive
    public ResponseResult updateSensitive(Sensitive sensitive) {
        updateById(sensitive);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult querySensitiveNameById(Integer id) {
        Sensitive byId = this.getById(id);
        Map<String, String> map = new HashMap<>();
        map.put("sensitive", byId.getSensitive());
        return ResponseResult.ok(map);
    }
}
